var supplierObj = [];
var limit = 15;
var tid;
var orgObj = convertOrgToObj(orgList);
var weightObj = convertEnumToObj(weightList);
var carTypeObj = convertEnumToObj(carTypeList);
layui.use(['laypage', 'table', 'form', 'laydate'], function(){
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#search-startTime',
        type: 'datetime'
    });
    laydate.render({
        elem: '#search-endTime',
        type: 'datetime'
    });
    let todoCols = [
        [
            {field: 'id', title: '标识', width:80, align: 'center', hide: true}
            ,{field: 'code', title: '订单号', width: '140', align: 'center',templet: function (val) {
                var code = val.code;
                if (!isNull(val.supplierOrderId)) {
                    code = val.supplierOrderId;
                }
                return '<div style="white-space: pre-wrap;">' + code + '</div>';
            }}
            ,{field: 'supplier', title: '过磅内容', width: '350', align: 'center',templet: function (val) {
                var supplier = val.supplier;
                return '<div style="white-space: pre-wrap;">' + supplier + '</div>';
            }}
            ,{field: 'material', title: '货物', width: '120', align: 'center',templet: function (val) {
                var material = val.material;
                return '<div style="white-space: pre-wrap;">' + material + '</div>';
            }}
            ,{field: 'carType', title: '车辆类型', width: '80', align: 'center',templet: function (val) {
                if (isNull(val.carType)) {
                    return "";
                }
                return '<div style="white-space: pre-wrap;">' + carTypeObj[val.carType] + '</div>';
            }}
            ,{field: 'carCode', title: '车辆代号', width: '90', align: 'center',templet: function (val) {
                if (isNull(val.carCode)) {
                    return "";
                }
                return '<div style="white-space: pre-wrap;">' + val.carCode + '</div>';
            }}
            ,{field: 'carNo', title: '车牌号', width: '100', align: 'center',templet: function (val) {
                if (isNull(val.carNo)) {
                    return "";
                }
                return '<div style="white-space: pre-wrap;">' + val.carNo + '</div>';
            }}
            ,{field: 'gross', title: '毛重', width:'80', align: 'center',templet:function (val) {
                var gross = val.gross;
                if (gross != null) {
                    gross = parseFloat(gross / 1000).toFixed(2);
                    return gross;
                }
                return 0;
            }}
            ,{field: 'netWeight', title: '净重', width: '80', align: 'center',templet:function (val) {
                var netWeight = val.netWeight;
                if (netWeight != null) {
                    netWeight = parseFloat(netWeight / 1000).toFixed(2);
                    return netWeight;
                }
                return 0;
            }}
            ,{field: 'tare', title: '皮重', width: '80', align: 'center',templet:function (val) {
                var tare = val.tare;
                if (tare != null) {
                    tare = parseFloat(tare / 1000).toFixed(2);
                    return tare;
                }
                return 0;
            }}
            ,{field: 'inTime', title: '入场时间', width: '140', align: 'center',templet: function (val) {
                if (isNull(val.inTime)) {
                    return "";
                }
                return '<div style="white-space: pre-wrap;">' + val.inTime + '</div>';
            }}
            ,{field: 'outTime', title: '出场时间', width: '140', align: 'center',templet: function (val) {
                if (isNull(val.outTime)) {
                    return "";
                }
                return '<div style="white-space: pre-wrap;">' + val.outTime + '</div>';
            }}
            ,{field: 'weightType', title: '称重类型', width: '90', align: 'center',templet: function (val) {
                //3商砼退料 4砼过磅 5登记皮重
                if (val.weightType == 3)
                    return '商砼退料';
                else if (val.weightType == 4)
                    return '砼过磅';
                else if (val.weightType == 5)
                    return '登记皮重';
            }}
            ,{field: 'remark', title: '备注', width: '180', align: 'center',templet: function (val) {
                if (isNull(val.remark)) {
                    return "";
                }
                return '<div style="white-space: pre-wrap;">' + val.remark + '</div>';
            }}
            ,{field: 'creator', title: '创建人', width: '5%', align: 'center'}
            ,{field: 'createTime', title: '创建时间', width: '10%', align: 'center'}
            ,{fixed: 'right', title:'操作', width: 80, align:'center', templet:function (val) {
                var str = '<a lay-event="detail" href="javascript:void(0)" class="layui-btn layui-btn-xs layui-btn-primary">详情</a>';
                // if (val.status == 0){
                //     str += '<a lay-event="delete" href="javascript:void(0)" class="layui-btn layui-btn-xs layui-btn-danger">禁用</a>';
                // }
                return str;
            }}
        ]
    ];

    table.render({
        elem: '#body-list'
        ,url: '/material-weight/carpage'
        ,method: 'post'
        ,page: true
        ,contentType: 'application/json'
        ,cols: todoCols
        ,limit: limit
        ,limits: [15,30,50,100]
        ,request: {
            pageName: 'curr' //页码的参数名称，默认：page
            ,limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        ,where: {
            offset: 0,
            limit: limit
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            console.log(res);
            if (res.code == 0 && res.data != null) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            } else {
                return {
                    "code": res.code, //解析接口状态
                    "msg": '没有任何数据', //解析提示文本
                    "count": 0, //解析数据长度
                    "data": '' //解析数据列表
                };
            }
        }
    });

    //监听行工具事件
    table.on('tool(body-list)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            ,layEvent = obj.event; //获得 lay-event 对应的值
        if(layEvent === 'delete'){
            var id = data.id;
            var status = data.status;
            var tip = '是否要启用？';
            if (status == 0)
                tip = '是否要禁用？';
            layer.confirm(tip, {
                btn: ['确定', '取消']
            }, function(index, layero){
                var updateStatus = 0;
                if (status == 0)
                    updateStatus = 1;
                var option =  '{"id": '+id+', "status":'+updateStatus+'}';
                save(option, "/system/sysLine/updatestatus");
            }, function(index){
            });
        }else if(layEvent === 'detail'){
            $('.updateModal').show();
            $(".update-code").val(data.code);
            $(".update-driver").val(data.driver);
            $(".update-phone").val(data.phone);
            $(".update-carType").val(data.carType);
            $(".update-weightType").val(data.weightType);
            $(".update-carNo").val(data.carNo);
            $(".update-gross").val(data.gross);
            $(".update-netWeight").val(data.netWeight);
            $(".update-tare").val(data.tare);
            $(".update-inDevice").val(data.inDevice);
            $(".update-outDevice").val(data.outDevice);
            $(".update-isPrint").val(data.isPrint);
            $(".update-isDisplay").val(data.isDisplay);
            $(".update-inTime").val(data.inTime);
            $(".update-outTime").val(data.outTime);
            $(".update-outTime").val(data.outTime);
            $(".update-remark").val(data.remark);
            $(".update-creator").val(data.creator);
            form.render();
        }
    });

    form.on('select(search-weightType)', function(data) {
        $(".btn-query").trigger("click");
    });

    $('.btn-query').on('click', function () {//搜索按钮
        var searchVal = $('.search-text').val();
        var startTime = $('.search-startTime').val();
        var endTime = $('.search-endTime').val();
        var weightType = $('.search-weightType').val();
        var option = {page:1,limit:limit};
        if (!isNull(searchVal))
            option['searchVal'] = searchVal;
        if (!isNull(startTime))
            option['startTime'] = startTime;
        if (!isNull(endTime))
            option['endTime'] = endTime;
        if (!isNull(weightType))
            option['weightType'] = weightType;
        table.reload('body-list', {
            where: option
        });

    });

    $(".updateModal .close").on("click", function() {
        $(".updateModal").hide();
        $(".updateModal input").val("");
        $(".updateModal select").val("");
        $(".updateModal textarea").val("");
        $(".qrcode-show").hide();
        form.render();
        tid = -1;
    });


    function save(option, url) {//新增或修改
        var loading = layer.load(1);
        console.log(option);
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            contentType: 'application/json',
            data: option,
            timeout: 30000,
            success: function(res) {
                layer.close(loading);
                if(res.code == 0) {
                    layer.msg("保存成功!");
                    table.reload('body-list', { where: {offset:0,curr:1,
                            limit: limit,
                            orgId:$('.search-origin').val() }});
                    $(".updateModal").hide();
                    $(".updateModal input").val("");
                    $(".updateModal select").val("");
                    $(".updateModal textarea").val("");
                    $(".addModal").hide();
                    $(".addModal input").val("");
                    $(".addModal select").val("");
                    $(".addModal textarea").val("");
                    form.render();
                } else {
                    layer.msg(res.msg);
                    return;
                }
            },
            error: function(res) {
                layer.msg("请求失败，请稍后再试！");
                layer.close(loading);
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    layer.msg("请求超时，请稍后再试！");
                    layer.close(loading);
                    return;
                }
            }
        })
    }


    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            $(".btn-query").trigger("click");
        }
    });
});